package aiku.numericalcontroloa.Controller.Die;

import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Model.Dto.DieOrderDto;
import aiku.numericalcontroloa.Model.Entity.DieOrder;
import aiku.numericalcontroloa.Service.DieOrderService;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

/**
 * @author 马文杰
 * @version 1.0
 */
@RestController
@RequestMapping("/dieOrder")
public class DieOrderController {
    @Autowired
    private DieOrderService dieOrderService;

    /**
     * 创建订单
     */
    @RequestMapping("/createOrder")
    public Result createOrder(@RequestBody DieOrderDto dieOrder) {
        return dieOrderService.createOrder(dieOrder);
    }

    /**
     * 管理修改订单信息
     */
    @RequestMapping("/updateOrder")
    public Result updateOrder(@RequestBody DieOrderDto dieOrder) {
        return dieOrderService.updateOrder(dieOrder);
    }

    @RequestMapping("/updateRoot")
    public Result updateOrder(@RequestBody DieOrder dieOrder) {
        return dieOrderService.updateRoot(dieOrder);
    }

    /**
     * 管理分页查询订单信息
     */
    @RequestMapping("/getOrderList")
    public Result getOrderList(@RequestBody DieOrderDto dieOrder) {
        return dieOrderService.getOrderList(dieOrder);
    }

    /**
     * 修改绘图,装刀,装线,切割,打包
     */
    @RequestMapping("/updateOrderStatus")
    public Result updateOrderStatus(@RequestBody DieOrderDto dieOrder) {
        return dieOrderService.updateOrderStatus(dieOrder);
    }

    /**
     * 统计当前登录用户的订单信息
     */
    @RequestMapping("/getOrderListByUser")
    public Result getOrderListByUser(@RequestParam("startDate") String start,
    @RequestParam("endDate") String end, @RequestParam("select") Integer select, @RequestParam("name") String name,
            @RequestParam("page") Integer page, @RequestParam("size") Integer size) {
                LocalDate startDate = null, endDate = null;
                if (!start.equals("") || !end.equals("")) {
                    startDate = LocalDate.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"));
                    endDate = LocalDate.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX"));
                }
        return dieOrderService.getOrderListByUser(startDate, endDate,select, name, page, size);
    }

    /**
     * 查询收益排行榜
     */
    @RequestMapping("/getIncomeRanking")
    public Result getIncomeRanking(String yearOrMonth) {
        return dieOrderService.getIncomeRanking(yearOrMonth);
    }

    /**
     * 通过订单ID查询该自己订单提成
     */
    @RequestMapping("/getOrderCommission")
    public Result getOrderCommission(@RequestParam("customId") String customId) {
        return dieOrderService.getOrderCommission(customId);
    }

    /**
     * 通过序号,客户名称,版号,产品名称模糊查询订单信息
     */
    @RequestMapping("/getOrderListByLike")
    public Result getOrderListByLike(@RequestParam("name") String name, @RequestParam("type") Integer type,
            @RequestParam("page") Integer page, @RequestParam("size") Integer size,
            @RequestParam("startDate") String startDates, @RequestParam("endDate") String endDates) {
        LocalDate startDate;
        LocalDate endDate;
        if (startDates.equals("")) {
            startDate = null;
        } else {
            startDate = LocalDate.parse(startDates, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        if (endDates.equals("")) {
            endDate = null;
        } else {
            endDate = LocalDate.parse(endDates, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        return dieOrderService.getOrderListByLike(name, type, page, size, startDate, endDate);
    }

    /**
     * 通过序号,客户名称,版号,产品名称模糊查询订单信息,查询当前用户自己的信息
     */
    @RequestMapping("/getOrderListByLikeAndUser")
    public Result getOrderListByLikeAndUser(@RequestParam("name") String name, @RequestParam("page") Integer page,
            @RequestParam("size") Integer size) {
        return dieOrderService.getOrderListByLikeAndUser(name, page, size);
    }

    @RequestMapping("/successOrders")
    public Result successOrders(@RequestParam("id") String id) {
        return dieOrderService.successOrders(id);
    }

    @RequestMapping("/deleteOrders")
    public Result deleteOrders(@RequestParam("id") String id) {
        return dieOrderService.deleteOrders(id);
    }

    /**
     * 获取刀模订单统计
     * @param yearOrMonth 年份或月份 (不带月份则为年，带月份为月)
     */
    @RequestMapping("/getStatistics")
    public Result getStatistics(String yearOrMonth) {
        return dieOrderService.getStatistics(yearOrMonth);
    }

    @GetMapping("/getAllOrderListByUser")
    public Result getAllOrderListByUser(@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX") LocalDate startDate, @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX") LocalDate endDate, Integer select, String name) {
        return dieOrderService.getAllOrderListByUser(startDate, endDate, select, name);
    }

}
